package project.parser.ast.stats;

import project.environments.StaticEnv;
import project.parser.TypecheckException;
import project.parser.ast.exps.Exp;
import project.parser.ast.types.PrimType;
import project.parser.ast.types.Type;

public abstract class ConFlow implements Stat {
	protected final Exp cond;
	protected final Stat body;

	public ConFlow(Exp cond, Stat body) {
		if (cond == null)
			throwException("condition cannot be null");
		this.cond = cond;
		this.body = body;
	}

	@Override
	public void typecheck(StaticEnv senv) {
		Type condType = cond.typecheck(senv);
		if (condType != PrimType.BOOLEAN_TYPE)
			throwException("condition have to be a boolean expression");
		body.typecheck(senv);
	}

	public abstract String condName();

	private void throwException(String message) {
		throw new TypecheckException("'" + condName() + "' " + message);
	}
}
